﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;

public static class WebBrowserFuncs
{
    private static  bool webCompleted = false;
    public static HtmlElementCollection GetElementsByTag(WebBrowser wb, string Tag)
    {
        return wb.Document.GetElementsByTagName(Tag);
    }

    public static string GetValueById(WebBrowser wb, string name)
    {
//        HtmlElementCollection ems = new HtmlElementCollection();
        HtmlElement em;
        em = wb.Document.GetElementById(name);
        if (em == null) return "";
        return em.InnerHtml;
    }
    public static void WaitBrowser(WebBrowser wb )
    {
        wb.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
        webCompleted = false;
        while (!webCompleted)
        {
            System .Threading .Thread .Sleep  (500);
            Application.DoEvents();
        }
    }

    private static  void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
    {
        webCompleted = true;
    }
    public static string GetURLfromOuterHtml(string ohtml,string currentPageUrl)
    {
        int hrefPos;
        int hrefEndPos;
        string urlChar;
        string urlGot;
        string currentDir;
        currentDir = currentPageUrl.Substring(0, currentPageUrl.LastIndexOf('/')+1);

        hrefPos = ohtml.IndexOf("href=");
        if (hrefPos > 0)
        {
            urlChar = ohtml.Substring(hrefPos + 5, 1);
            hrefEndPos = ohtml.IndexOf(urlChar, hrefPos + 6);
            urlGot = ohtml.Substring(hrefPos + 6, hrefEndPos - hrefPos - 6);
            if (urlGot.StartsWith("http://")) return urlGot;
            else return currentDir + urlGot;
        }
        else return "";
        
    }

    public static DataTable ConvertTbodyToDataTable(HtmlElement hem)
    {
        DataTable dt = new DataTable();

        foreach (HtmlElement em1 in hem.GetElementsByTagName("thead")[0].GetElementsByTagName("th"))
        {
            dt.Columns.Add(em1.InnerText,typeof (string ));
        }

        foreach (HtmlElement em1 in hem.GetElementsByTagName("tr"))
        {
            HtmlElementCollection ec = em1.GetElementsByTagName("td");
            //if (ec.Count != 4) continue;

            dt.Rows.Add (new object []
            {
                ec [0].InnerText ,
                ec[1].InnerText ,
                ec[2].InnerText ,
                ec[3].InnerText 
            });
        }
        

        return dt;
    }

    public static DataTable ConvertTbodyToDataTable(HtmlElement hem,bool GeneralTable)
    {
        DataTable dt = new DataTable();
        string strURLTmp = "";
        foreach (HtmlElement em1 in hem.GetElementsByTagName("tr"))
        {
            HtmlElementCollection ec = em1.GetElementsByTagName("td");
            if (dt.Columns.Count < 1)
            {
                for (int i = 1; i < ec.Count + 1; i++)
                    dt.Columns.Add();
            }
            //if (ec.Count != 4) continue;
            List<object> ls = new List<object>();
            strURLTmp = "";
            for (int i = 1; i < ec.Count; i++)
            {
                if (ec[i].GetElementsByTagName("a").Count > 0) strURLTmp = ec[i].GetElementsByTagName ("a")[0].GetAttribute ("href");
                ls.Add(ec[i].InnerText);
            }
            if (strURLTmp != "") ls.Add(strURLTmp);
            dt.Rows.Add(ls.ToArray ());
        }


        return dt;
    }
    
}
